package com.wtowto.pdfreader;


import android.content.Context;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

import com.lidong.pdf.PDFView;
import com.lidong.pdf.listener.OnDrawListener;
import com.lidong.pdf.listener.OnLoadCompleteListener;
import com.lidong.pdf.listener.OnPageChangeListener;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.common.Constants;
import com.taobao.weex.dom.WXDomObject;
import com.taobao.weex.ui.component.WXComponent;
import com.taobao.weex.ui.component.WXComponentProp;
import com.taobao.weex.ui.component.WXVContainer;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXUtils;


/**
 * Created by aj_wenlan on 2018/6/7.
 */

public class PdfReader extends WXComponent implements OnPageChangeListener,OnLoadCompleteListener,OnDrawListener {

    private PDFView pdfView;
    private static final int CODE_DOWNLOAD_JSON = 0x01;
    private Handler mHandler = null;
    private String uri = null;
    private String pdfTitle = null;

    public PdfReader(WXSDKInstance instance, WXDomObject dom, WXVContainer parent) {
        super(instance, dom, parent);
    }

    @Override
    protected View initComponentHostView(@NonNull Context context){
        WXLogUtils.e("initComponentHostView");
        pdfView = new PDFView(context,null);
//        displayFromFile(uri,pdfTitle);
        return pdfView;
    }

//        WXLogUtils.e("initComponentHostView");
//        setPath(this.uri);
//        setTitle(this.pdfName);
//        mHandler = new Handler(){
//            @Override
//            public void handleMessage(Message msg){
//                switch (msg.what){
//                    case CODE_DOWNLOAD_JSON:
//                        WXLogUtils.e("Handler =" + uri);
//                        displayFromFile(uri,pdfName);
//                        break;
//                }
//                super.handleMessage(msg);
//            }
//        };

//    @Override
//    protected boolean setProperty(String key,Object param){
//        WXLogUtils.e("setProperty =" + uri);
//        switch(key){
//            case Constants.Name.SRC:
//                this.uri = WXUtils.getString(param,"");
//                setPath(this.uri);
//                displayFromFile(uri,pdfName);
//                break;
//        }
//        return super.setProperty(key,param);
//    }

//    @WXComponentProp(name="src")
//    public void setPath(String src){
//        uri = src;
//        WXLogUtils.e("uri =" + uri);
//    }

    @JSMethod(uiThread = true)
    public View setPdf(String src,String title){
        WXLogUtils.e("setPdf = " + src + "title:" + title);
        displayFromFile(src,title);
        return pdfView;
    }

//    @WXComponentProp(name="title")
//    public void setTitle(String title){
//        pdfTitle = title;
//        WXLogUtils.e("title =" + pdfTitle);
//    }

    private void displayFromFile(String fileUrl, String fileName) {
//        showProgress();
        WXLogUtils.e("fileUrl" + fileUrl);
        pdfView.fileFromLocalStorage(this,this,this,fileUrl,fileName);
    }


    @Override
    public void onPageChanged(int page, int pageCount) {
        WXLogUtils.e("onPageChanged");
    }

    @Override
    public void onLayerDrawn(Canvas canvas, float pageWidth, float pageHeight, int displayedPage) {
        WXLogUtils.e("onLayerDrawn");
    }

    @Override
    public void loadComplete(int nbPages) {
        WXLogUtils.e("loadComplete");
    }
}
